<?php

/**
 * tema actions.
 *
 * @package    camaras
 * @subpackage tema
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class temaActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
  	$this->form = new TemaForm();
	
    $this->tema_list = Doctrine::getTable('Tema')
      ->createQuery('a')
	  ->where('deleted = ?', 0)
	  ->addWhere('cd_tema_pai IS NULL')
	  ->orderBy('name')
      ->execute();

    $this->tema_filho_list = Doctrine::getTable('Tema')
      ->createQuery('a')
	  ->where('deleted = ?', 0)
	  ->addWhere('cd_tema_pai IS NOT NULL')
	  ->orderBy('name')
      ->execute();

    $this->tema_neto_list = Doctrine::getTable('Tema')
      ->createQuery('a')
	  ->where('deleted = ?', 0)
	  ->addWhere('cd_tema_pai IS NOT NULL')
	  ->orderBy('name')
      ->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->tema = Doctrine::getTable('Tema')->find(array($request->getParameter('cd_tema')));
    $this->forward404Unless($this->tema);
  }

  public function executeNew(sfWebRequest $request)
  {
	if(!$this->getUser()->getAttribute('gerente',null,'usr')){
	  $this->getUser()->setFlash('error', sfConfig::get('app_msg_cadastro_exclusivo_gerente'));
	  $this->redirect('@homepage');
	} else {
      $this->form = new TemaForm();
	}
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new TemaForm();
    
	$this->processForm($request, $this->form);

    $this->redirect('tema/index');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($tema = Doctrine::getTable('Tema')->find(array($request->getParameter('cd_tema'))), sprintf('Object tema does not exist (%s).', array($request->getParameter('cd_tema'))));
    $this->form = new TemaForm($tema);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($tema = Doctrine::getTable('Tema')->find(array($request->getParameter('cd_tema'))), sprintf('Object tema does not exist (%s).', array($request->getParameter('cd_tema'))));
    $this->form = new TemaForm($tema);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($tema = Doctrine::getTable('Tema')->find(array($request->getParameter('cd_tema'))), sprintf('Object tema does not exist (%s).', array($request->getParameter('cd_tema'))));
    $tema->delete();

    $this->getUser()->setFlash('notice', sfConfig::get('app_msg_confirmacao'));			
    $this->redirect('tema/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()));
    if ($form->isValid())
    {
      $tema = $form->save();
      $this->getUser()->setFlash('notice', sfConfig::get('app_msg_confirmacao'));			
    } else {
	  $this->getUser()->setFlash('error', 'Tema em branco ou já existente!');			
    } 
	$this->redirect('tema/index');
  }
}
